import ajax from './ajax'

const _processDownload = function(res, filename) {
  let link = document.createElement('a');
  link.href = window.URL.createObjectURL(res.data);
  link.download = filename;
  link.click();
}
/*
const account = {   
  login (code, password, onSuccess) {
    Ajax
    .post("/login", {"code":code, "password":password})
    .then(onSuccess); 
  }
}
*/

const CHART_PREDICT = () => { 
  return {
    legend: { bottom: 0, data: ['预测得分','样本人数'] },
    tooltip: {trigger: 'axis'},
    grid: { top: '30',left: '10', right: '10', bottom: '30', containLabel: true },
    xAxis: { type: 'category', data: /* predictChart.labels */ [] },
    yAxis: [
      {
          name: '预测得分',
          type: 'value',
          interval: /*[[${predictChart.scoreInterval}]]*/ 1,
          min: /*[[${predictChart.scoreMin}]]*/ 0,
          max: /*[[${predictChart.scoreMax}]]*/ 10
      },
      {
          name: '样本人数',
          type: 'value',
          interval: /*[[${predictChart.ratingCountInterval}]]*/ 100,
          min: /*[[${predictChart.ratingCountMin}]]*/ 500,
          max: /*[[${predictChart.ratingCountMax}]]*/ 1500
      }
    ],
    series: [
        {
            name:'预测得分', type:'line', smooth: true, yAxisIndex: 0,
            data:/*[[${predictChart.scoreList}]]*/ []
        },
        {
            name:'样本人数', type:'line', smooth: true, yAxisIndex: 1,
            data:/*[[${predictChart.ratingCountList}]]*/ []
        }
    ]
  };
};


export default {
  downloadComments(id, min, max, filename) {
    ajax({
      url:'/douban/movie/' + id + '/comments/excel?min=' + min + '&max=' + max,
      method:'GET',
      responseType:'blob'
    }).then(res=>{
      _processDownload(res, filename);
    });
  },

  loadComments(id, offset, onSuccess) {
    ajax.get('/douban/movie/' + id + '/comments/latest?limit=5&offset=' + offset)
        .then(onSuccess); 
  },

  loadKeywords(id, min, max, onSuccess) {
    ajax.get('/douban/movie/keyword/' + id + '?limit=50&min=' + min + '&max=' + max)
        .then(onSuccess); 
  },

  loadInterests(onSuccess) {
    ajax.get('/douban/movie/interests')
        .then(onSuccess); 
  },
  
  loadOneMovie(id, onSuccess) {
    ajax.get('/douban/movie/' + id).then(onSuccess);
  },

  searchMovie(query, onSuccess) {
    if(query) {
      ajax.get('/douban/movie/search?q=' + query)
          .then(onSuccess);  
    }
  },

  doMarks(id, markType, checked) {
    ajax.post('/douban/movie/' + id + '/marks', {"type":markType, "marked":checked});
  },

  // deprecated
  loadPredicts(id, onSuccess) {
    ajax.get('/douban/movie/' + id + '/predicts')
        .then(onSuccess); 
  },

  loadScoresChanged(id, onSuccess) {
    ajax.get('/douban/movie/' + id + '/scores-milestone')
        .then(onSuccess); 
  },

  loadScoresByDate(id, onSuccess) {
    ajax.get('/douban/movie/' + id + '/scores')
        .then(onSuccess); 
  },

  loadScoresByHour(id, onSuccess) {
    ajax.get('/douban/movie/' + id + '/scores?type=hour')
        .then(onSuccess); 
  },

  loadUserRatings(id, onSuccess) {
    ajax.get('/douban/movie/' + id + '/ratings').then(onSuccess);
  },

  loadMovieInterests(onSuccess) {
    ajax.get('/douban/movie/interests').then(onSuccess);    
  },

  loadMovieMarks(id, onSuccess) {
    ajax.get('/douban/movie/' + id + '/marks').then(onSuccess);    
  },

  loadMarks(id, onSuccess) {
    ajax.get('/douban/movie/' + id + '/marks').then(onSuccess);    
  },

  doMovieMark(id, markType, checked) {
    ajax.post('/douban/movie/' + id + '/marks', {"type":markType, "marked":checked});
  }
};